package cn.tansanqinger.controller.knowledge;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tansanqinger.service.knowledge.IKnowledgeMenuService;
import cn.tansanqinger.util.Tools;

/**
 * 对知识目录进行管理
 * @author C3H2
 * @time 2021.03
 */

@Controller
@RequestMapping("/admin/knowledge")
public class KnowledgeMenuController {
	
	@Autowired
	private IKnowledgeMenuService service;
	
	@RequestMapping("/list6")
	public ModelAndView list(HttpServletRequest request, ModelAndView mv) {
		mv.setViewName("knowledge/list6");
		return mv;
	}
	
	@RequestMapping("/findListData6")
	@ResponseBody
	public String findListData(HttpServletRequest request) {
		try {
			return new ObjectMapper().writeValueAsString(service.selectMessage(request));
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 添加信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/goAdd6")
	public ModelAndView goAdd(HttpServletRequest request, ModelAndView mv) {
		String ctier = Tools.isEmpty(request.getParameter("ctier"));
		String handSn = Tools.isEmpty(request.getParameter("handSn"));
		mv.addObject("serialNumber", service.serialNumber(Integer.parseInt(ctier)+1));
		mv.addObject("handSn", handSn);
		mv.addObject("ctier", ctier);
		mv.setViewName("knowledge/goAdd6");
		return mv;
	}
	
	/**
	 * 修改状态
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateWeekend6")
	public @ResponseBody Map<String, Object> updateWeekend(HttpServletRequest request) {
		return service.updateStatus(request.getParameter("id"));
	}
	
	/**
	 * 编辑信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/edit6")
	public ModelAndView edit(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("goods", service.selectIdOne(Tools.isEmpty(request.getParameter("id"))));
		mv.setViewName("admin/edit6");
		return mv;
	}
	
	/**
	 * 添加数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/addMenu6")
	public @ResponseBody Map<String, Object> addMenu(HttpServletRequest request) {
		return service.add(request);
	}
	
	/**
	 * 数据删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete6")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request){
		return service.delete(Tools.isEmpty(request.getParameter("cid")), Tools.isEmpty(request.getParameter("cno")));
	}
	
	/**
	 * 修改信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/modification6")
	@ResponseBody
	public Map<String, Object> modification(HttpServletRequest request){
		return service.modification(request);
	}
}
